import { useWebConfigStore,useUserStore,useRefs } from "@/stores";
import {$http} from './http';
import {$message} from './message';
// #ifdef H5
import system from './h5/system';
// #endif
// #ifdef MP-WEIXIN
import system from './mp-weixin/system';
// #endif
const buildOptions=(options:any)=>{
    // #ifdef H5
    const appid=system.appid();
    if(options.url.indexOf('?')>-1){
        options.url+='&';
    }else{
        options.url+='?';
    }
    options.url+=`appid=${appid}`;
    // #endif
    return options;
}
export const $page={
	open:(params:UniApp.NavigateToOptions | string)=>{
		let options:UniApp.NavigateToOptions={
			url:''
		};
		if(typeof params==='string'){
			options.url=params;
		}else{
			options=Object.assign(options,params);
		}
		uni.navigateTo(buildOptions(options));
	},
	redirect:(params:UniApp.RedirectToOptions | string)=>{
		let options:UniApp.RedirectToOptions={
			url:''
		};
		if(typeof params==='string'){
			options.url=params;
		}else{
			options=Object.assign(options,params);
		}
		uni.redirectTo(buildOptions(options))
	},
	re:(params:UniApp.ReLaunchOptions | string)=>{
		let options:UniApp.ReLaunchOptions={
			url:''
		};
		if(typeof params==='string'){
			options.url=params;
		}else{
			options=Object.assign(options,params);
		}
		uni.reLaunch(buildOptions(options))
	},
	switch:(params:UniApp.SwitchTabOptions|string)=>{
		let options:UniApp.SwitchTabOptions={
			url:''
		};
		if(typeof params==='string'){
			options.url=params;
		}else{
			options=Object.assign(options,params);
		}
		uni.switchTab(buildOptions(options));
	},
	title:(title?:string,subtitle?:string)=>{
		const webConfigStore=useWebConfigStore();
		const {WEBCONFIG}=useRefs(webConfigStore);
		const fastTitle=title||WEBCONFIG.value?.web_name||system.name();
		const lastTitle=subtitle!==undefined?subtitle:'';
		uni.setNavigationBarTitle({
			title:fastTitle+lastTitle
		})
	},
	ads:(item:any)=>{
		switch(item.page){
			case "GAME":
				$page.open(`/pages/index/game?game=${item.url}`);
				break;
			case "ARTICLE":
				$page.open(`/pages/article/content?id=${item.alias_id}`);
				break;
			case "SWITCH":
				$page.switch(item.url);
				break;
			case "CUSTOM":
				// #ifdef H5
				globalThis.open(item.url);
				// #endif
				break;
		}
	},
	shareMessage(options:any){
		const webConfigStore=useWebConfigStore();
		const {WEBCONFIG}=useRefs(webConfigStore);
		const userStore=useUserStore();
		let obj:any={
			...options
		};
		obj.title=WEBCONFIG.value?.share_title||options.title||system.name();
		obj.imageUrl=WEBCONFIG.value?.share_image||options.imageUrl||'';
		if(userStore.hasLogin()){
			const {USERINFO}=useRefs(userStore);
			if(obj.path.indexOf('?')>-1){
				obj.path+='&';
			}else{
				obj.path+='?';
			}
			obj.path+=`puid=${USERINFO.value.id}`;
		}
		return obj;
	},
	shareTimeline(options:any){
		const webConfigStore=useWebConfigStore();
		const {WEBCONFIG}=useRefs(webConfigStore);
		const userStore=useUserStore();
		let obj:any={
			...options
		};
		obj.title=WEBCONFIG.value?.share_title||options.title||system.name();
		obj.imageUrl=WEBCONFIG.value?.share_image||options.imageUrl||'';
		if(userStore.hasLogin()){
			const {USERINFO}=useRefs(userStore);
			if(obj.path.indexOf('?')>-1){
				obj.path+='&';
			}else{
				obj.path+='?';
			}
			obj.path+=`puid=${USERINFO.value.id}`;
		}
		return obj;
	},
}
